-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
RestWebServiceTests.cs
201 lines (176 loc) · 7.8 KB
/
RestWebServiceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.IO;
using System.Net;
using System.Text;
using NUnit.Framework;
using ServiceStack.Text;
using ServiceStack.Web;
using ServiceStack.WebHost.IntegrationTests.Services;
namespace ServiceStack.WebHost.IntegrationTests.Tests
{
[TestFixture]
public class RestWebServiceTests
: RestsTestBase
{
[Test]
public void Can_call_EchoRequest_with_AcceptAll()
{
var response = GetWebResponse(ServiceClientBaseUri + "/echo/1/One", "*/*");
var contents = GetContents(response);
Assert.That(contents, Is.Not.Null);
Assert.That(contents.Contains("\"id\":1"));
Assert.That(contents.Contains("\"string\":\"One\""));
}
[Test]
public void Can_call_EchoRequest_with_AcceptJson()
{
var response = GetWebResponse(ServiceClientBaseUri + "/echo/1/One", MimeTypes.Json);
AssertResponse<EchoRequest>(response, MimeTypes.Json, x =>
{
Assert.That(x.Id, Is.EqualTo(1));
Assert.That(x.String, Is.EqualTo("One"));
});
}
[Test]
public void Can_call_EchoRequest_with_AcceptXml()
{
var response = GetWebResponse(ServiceClientBaseUri + "/echo/1/One", MimeTypes.Xml);
AssertResponse<EchoRequest>(response, MimeTypes.Xml, x =>
{
Assert.That(x.Id, Is.EqualTo(1));
Assert.That(x.String, Is.EqualTo("One"));
});
}
[Test]
public void Can_call_EchoRequest_with_AcceptJsv()
{
var response = GetWebResponse(ServiceClientBaseUri + "/echo/1/One", MimeTypes.Jsv);
AssertResponse<EchoRequest>(response, MimeTypes.Jsv, x =>
{
Assert.That(x.Id, Is.EqualTo(1));
Assert.That(x.String, Is.EqualTo("One"));
});
}
[Test]
public void Can_call_EchoRequest_with_QueryString()
{
var response = GetWebResponse(ServiceClientBaseUri + "/echo/1/One?Long=2&Bool=True", MimeTypes.Json);
AssertResponse<EchoRequest>(response, MimeTypes.Json, x =>
{
Assert.That(x.Id, Is.EqualTo(1));
Assert.That(x.String, Is.EqualTo("One"));
Assert.That(x.Long, Is.EqualTo(2));
Assert.That(x.Bool, Is.EqualTo(true));
});
}
private HttpWebResponse EmulateHttpMethod(string emulateMethod, string useMethod)
{
var webRequest = (HttpWebRequest)WebRequest.Create(ServiceClientBaseUri + "/echomethod");
webRequest.Accept = MimeTypes.Json;
webRequest.Method = useMethod;
webRequest.Headers[HttpHeaders.XHttpMethodOverride] = emulateMethod;
if (useMethod == HttpMethods.Post)
webRequest.ContentLength = 0;
var response = (HttpWebResponse)webRequest.GetResponse();
return response;
}
[Test]
public void Can_emulate_Put_HttpMethod_with_POST()
{
var response = EmulateHttpMethod(HttpMethods.Put, HttpMethods.Post);
AssertResponse<EchoMethodResponse>(response, MimeTypes.Json, x =>
Assert.That(x.Result, Is.EqualTo(HttpMethods.Put)));
}
[Test]
public void Can_emulate_Put_HttpMethod_with_GET()
{
var response = EmulateHttpMethod(HttpMethods.Put, HttpMethods.Get);
AssertResponse<EchoMethodResponse>(response, MimeTypes.Json, x =>
Assert.That(x.Result, Is.EqualTo(HttpMethods.Put)));
}
[Test]
public void Can_emulate_Delete_HttpMethod_with_GET()
{
var response = EmulateHttpMethod(HttpMethods.Delete, HttpMethods.Get);
AssertResponse<EchoMethodResponse>(response, MimeTypes.Json, x =>
Assert.That(x.Result, Is.EqualTo(HttpMethods.Delete)));
}
[Test]
public void Can_call_WildCardRequest_with_alternate_WildCard_defined()
{
var response = GetWebResponse(ServiceClientBaseUri + "/wildcard/1/aPath/edit", MimeTypes.Json);
AssertResponse<WildCardRequest>(response, MimeTypes.Json, x =>
{
Assert.That(x.Id, Is.EqualTo(1));
Assert.That(x.Path, Is.EqualTo("aPath"));
Assert.That(x.Action, Is.EqualTo("edit"));
Assert.That(x.RemainingPath, Is.Null);
});
}
[Test]
public void Can_call_WildCardRequest_WildCard_mapping()
{
var response = GetWebResponse(ServiceClientBaseUri + "/wildcard/1/remaining/path/to/here", MimeTypes.Json);
AssertResponse<WildCardRequest>(response, MimeTypes.Json, x =>
{
Assert.That(x.Id, Is.EqualTo(1));
Assert.That(x.Path, Is.Null);
Assert.That(x.Action, Is.Null);
Assert.That(x.RemainingPath, Is.EqualTo("remaining/path/to/here"));
});
}
[Test]
public void Can_call_WildCardRequest_WildCard_mapping_with_QueryString()
{
var response = GetWebResponse(ServiceClientBaseUri + "/wildcard/1/remaining/path/to/here?Action=edit", MimeTypes.Json);
AssertResponse<WildCardRequest>(response, MimeTypes.Json, x =>
{
Assert.That(x.Id, Is.EqualTo(1));
Assert.That(x.Path, Is.Null);
Assert.That(x.Action, Is.EqualTo("edit"));
Assert.That(x.RemainingPath, Is.EqualTo("remaining/path/to/here"));
});
}
[Test(Description = "Test for error processing empty XML request")]
public void Can_call_ResetMovies_mapping_with_empty_Xml_post()
{
var response = GetWebResponse(HttpMethods.Post, ServiceClientBaseUri + "/reset-movies", MimeTypes.Xml, 0);
AssertResponse<ResetMoviesResponse>(response, MimeTypes.Xml, x =>
{
});
}
[Test]
public void Can_POST_new_movie_with_FormData()
{
const string formData = "Id=0&ImdbId=tt0110912&Title=Pulp+Fiction&Rating=8.9&Director=Quentin+Tarantino&ReleaseDate=1994-11-24&TagLine=Girls+like+me+don't+make+invitations+like+this+to+just+anyone!&Genres=Crime%2CDrama%2CThriller";
var formDataBytes = Encoding.UTF8.GetBytes(formData);
var webRequest = (HttpWebRequest)WebRequest.Create(ServiceClientBaseUri + "/movies");
webRequest.Accept = MimeTypes.Json;
webRequest.ContentType = MimeTypes.FormUrlEncoded;
webRequest.Method = HttpMethods.Post;
webRequest.ContentLength = formDataBytes.Length;
webRequest.GetRequestStream().Write(formDataBytes, 0, formDataBytes.Length);
try
{
var response = (HttpWebResponse)webRequest.GetResponse();
AssertResponse<MovieResponse>(response, MimeTypes.Json, x =>
{
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created));
Assert.That(response.Headers["Location"], Is.EqualTo(ServiceClientBaseUri + "/movies/" + x.Movie.Id));
});
}
catch (WebException webEx)
{
if (webEx.Status == WebExceptionStatus.ProtocolError)
{
var errorResponse = ((HttpWebResponse)webEx.Response);
Console.WriteLine("Error: " + webEx);
Console.WriteLine("Status Code : {0}", errorResponse.StatusCode);
Console.WriteLine("Status Description : {0}", errorResponse.StatusDescription);
Console.WriteLine("Body:" + errorResponse.GetResponseStream().ReadToEnd());
}
throw;
}
}
}
}